package com.cwj.mansysback.entity;

import com.cwj.mansysback.dao.api.TeacherDao;
import com.cwj.mansysback.dao.impl.RanksDaoImpl;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;

import java.util.HashMap;
import java.util.List;

public class Teacher extends Account {

    private static HashMap<String,Rank> teaRankNames = null;

    private static HashMap<String,Rank> teaTnameMapRank = null;


    private  static final Integer RANK = 2;

    private String tname;

    private Double tsalary;

    private String rid;

    private Double rpiece;

    private Boolean tsex;

    private Integer tage;



    static {
        Teacher.updateStatic(true);
    }


    public Teacher() {

    }

    public Teacher( String tname,Double tsalary, String rid) {
//        this.tid = tid;
        this.tname = tname;
        this.tsalary = tsalary;
        this.rid = rid;
    }

    public Teacher(String aid, String password, Integer accrank, String tname, Double tsalary, String rid, Double rpiece) {
        super(aid, password, Teacher.RANK);
        this.tname = tname;
        this.tsalary = tsalary;
        this.rid = rid;
        this.rpiece = rpiece;
    }

    public Teacher(String aid, String password, Integer accrank, String tname, Double tsalary, String rid, Double rpiece, Boolean tsex, Integer tage) {
        super(aid, password, accrank);
        this.tname = tname;
        this.tsalary = tsalary;
        this.rid = rid;
        this.rpiece = rpiece;
        this.tsex = tsex;
        this.tage = tage;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public Double getTsalary() {
        return tsalary;
    }

    public void setTsalary(Double tsalary) {
        this.tsalary = tsalary;
    }

    public Double getRpiece() {
        return rpiece;
    }

    public void setRpiece(Double rpiece) {
        this.rpiece = rpiece;
    }

    public String getRid() {
        if(Teacher.teaTnameMapRank.containsKey(this.rid)){
            this.rid = Teacher.teaTnameMapRank.get(this.rid).getRid();
        }
        return rid;
    }

    public void setRid(String rid) {
        this.rid = rid;
    }

    public Boolean getTsex() {
        return tsex;
    }

    public void setTsex(Boolean tsex) {
        this.tsex = tsex;
    }

    public Integer getTage() {
        return tage;
    }

    public void setTage(Integer tage) {
        this.tage = tage;
    }

    @Override
    public void lastChange(){
        super.lastChange();
        this.rpiece=teaRankNames.get(this.rid).getRpiece();

        this.rid=Teacher.teaRankNames.get(this.rid).getRname();

    }


    public static HashMap<String, Rank> getTeaRankNames() {
        return teaRankNames;
    }

    public static void setTeaRankNames(HashMap<String, Rank> teaRankNames) {
        Teacher.teaRankNames = teaRankNames;
    }

    public static Integer getRANK() {
        return RANK;
    }


    private static void updateMap(){
        List<Rank>ranks = new RanksDaoImpl().selectALL();

        teaRankNames = new HashMap<>();
        teaTnameMapRank = new HashMap<>();

        for (Rank rank : ranks) {
            teaRankNames.put(rank.getRid(), rank);
            teaTnameMapRank.put(rank.getRname(),rank);
        }
    }

//    更新静态变量
    public static void updateStatic(boolean isUpdateMap){
        if(isUpdateMap) {
            Teacher.updateMap();
        }
    }

    @Override
    public String toString() {
        return super.toString()+"Teacher{" +
                "tname='" + tname + '\'' +
                ", tsalary=" + tsalary +
                ", rid='" + rid + '\'' +
                ", rpiece=" + rpiece +
                ", tsex=" + tsex +
                ", tage=" + tage +
                '}';
    }
}
